﻿using System;
using System.IO;
using System.Net;
//using System.Net.NetworkInformation;
//using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
//using System.Text;
//using System.Windows;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Xml;
//using System.Xml.Serialization;
//using System.Xml.Linq;

namespace Tools.Libraries
{
    public class CryptoData
    {
        private SecureString _pass;

        public CryptoData()
        {
            setPasswordValue();
        }

        private enum cryptoProperty
        {
            Key,
            IV
        }

        private void setPasswordValue()
        {
            char[] chars = { 'u', 'n', 'e', 'm', 'a', 'r', 'g', 'u', 'e', 'r', 'i', 't', 'e', 'a', 't', 'o', 'u', 'j', 'o', 'u', 'r', 's', 'l', 'a', 'f', 'r', 'i', 't', 'e' };
            _pass = new SecureString();
            foreach (char ch in chars)
                _pass.AppendChar(ch);
            _pass.MakeReadOnly();
        }

        private Byte[] getCryptoProperty(cryptoProperty prop, String password)
        {
            Byte[] key = null;
            Byte[] salt = System.Text.Encoding.ASCII.GetBytes("cryptoboum");
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);

            if (prop == cryptoProperty.Key)
            {
                key = pdb.GetBytes(32);
            }
            else if (prop == cryptoProperty.IV)
            {
                key = pdb.GetBytes(16);
            }

            return key;
        }

        public Byte[] encryptData(Byte[] bytesToEncrypt)
        {
            Byte[] retour = null;
            try
            {
                SymmetricAlgorithm alg = new RijndaelManaged();
                Byte[] _key = getCryptoProperty(cryptoProperty.Key, _pass.ToString());
                Byte[] _IV = getCryptoProperty(cryptoProperty.IV, _pass.ToString());

                alg.Key = _key;
                alg.IV = _IV;

                ICryptoTransform encryptor = alg.CreateEncryptor(_key, _IV);
                MemoryStream memoryStream = new MemoryStream();

                CryptoStream encStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                encStream.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);
                encStream.FlushFinalBlock();

                retour = memoryStream.ToArray();

                memoryStream.Close();
                encStream.Close();
            }
            catch (Exception)
            {
                retour = null;
            }
            finally
            {

            }

            return retour;
        }

        public Byte[] decryptData(Byte[] bytesToDecrypt)
        {
            Byte[] retour = null;

            Byte[] _key = getCryptoProperty(cryptoProperty.Key, _pass.ToString());
            Byte[] _IV = getCryptoProperty(cryptoProperty.IV, _pass.ToString());

            // Create the algorithm and specify the key and IV
            RijndaelManaged alg = new RijndaelManaged();
            alg.Key = _key;
            alg.IV = _IV;

            ICryptoTransform decryptor = alg.CreateDecryptor(_key, _IV);
            MemoryStream memoryStream = new MemoryStream(bytesToDecrypt);

            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            retour = new Byte[bytesToDecrypt.Length];
            int DecryptedCount = cryptoStream.Read(retour, 0, retour.Length);

            memoryStream.Close();
            cryptoStream.Close();

            return retour;
        }
    }

    public static class Hash
    {
        public static Byte[] GetSHA1Hash(string originalString)
        {
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(originalString);
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            return sha.ComputeHash(data);
        }

        //public static string GetSHA1Hash(string pathName)
        //{
        //    string strResult = "";
        //    string strHashData = "";

        //    byte[] arrbytHashValue;
        //    FileStream oFileStream = null;

        //    SHA1CryptoServiceProvider oSHA1Hasher = new SHA1CryptoServiceProvider();

        //    try
        //    {
        //        oFileStream = GetFileStream(pathName);
        //        arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
        //        oFileStream.Close();

        //        strHashData = System.BitConverter.ToString(arrbytHashValue);
        //        strHashData = strHashData.Replace("-", "");
        //        strResult = strHashData;
        //    }
        //    catch (System.Exception ex)
        //    {
        //    }

        //    return (strResult);
        //}

        //private static FileStream GetFileStream(string pathName)
        //{
        //    return (new FileStream(pathName, FileMode.Open,
        //                FileAccess.Read, FileShare.ReadWrite));
        //}
    }
}